#!/bin/bash -e

function info() {
echo Usage: `basename $0` 'ref_bed.prefix test_bed.prefix'
exit 1
}

while getopts  ":p:f:i:d:" opt; do
case  $opt  in
    p) out_prefix=$OPTARG;;
    f) suffix=$OPTARG;;
    i) bed=$OPTARG;;
    d) depth_filter=$OPTARG;;
    *) info;;
esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. $var

unset PYTHONPATH
. ~/../fei.yang/yn/tools/conda/bin/activate py36

prefix1=$1
prefix2=$2
ids=$2
k=11

cut -f1 -d' ' $prefix2.fam > $out_prefix.samples
num=`cat $out_prefix.samples|wc -l`
sample=$out_prefix.sample
q_file=$out_prefix.q
:>$q_file

for i in `seq $num`; do
    sed -n "${i}p" $prefix2.fam|cut -f1 -d' ' > $sample
    samp=`cat $sample`
    extract_sample_plink.sh -p$out_prefix.$num.$i $prefix2 $sample
    plink --bfile $prefix1 --bmerge $out_prefix.$num.$i.target.bed $out_prefix.$num.$i.target.bim $out_prefix.$num.$i.target.fam --make-bed --out $out_prefix.merge.$i
    admix0.sh -p$out_prefix.merge.$i -o$prefix1.$k.P $out_prefix.merge.$i
    grep -F $samp  $out_prefix.merge.$i.$k.1.q >> $q_file
done

q_bar1.py -p$out_prefix $q_file $out_prefix.samples



. $cmd_done
